package 串;
//顺序字符串
public class SeqString {
    private final int defaultSize =10;
    private int maxSize;
    private int count; //栈顶指示
    private char[] value; //一堆数据
    public SeqString() {
        initiate(defaultSize);
    }
    public SeqString(int sz) {
        initiate(sz);
    }
    private void initiate(int sz) {
        value=new char[sz];
        count=0;
    }

    //串的赋值,把t赋值给当前对象
    public void Assign(char[]t){
        for (int i = 0; i <t.length ; i++) {
            this.value[i] = t[i];
        }
        this.count=t.length;
    }
    //串长
    public int Length(){
        return count;
    }
    //判断是否相等
    public boolean Equal(SeqString s){
        //判断当前项和S的长度是否相等，相等在比较
        for (int i = 0; i < this.count; i++) {
            if (this.value[i]!=s.value[i]){
                return false;
            }

        }
        return true;
    }
    //连接串
    public SeqString Concat(SeqString s){
        for (int i = 0; i <s.count; i++) {
            value[count+i] = s.value[i];

        }
        this.count= this.count+s.count;
        return  this;
    }
    //求子串,将子串作为全新的对象返回
    public SeqString SubStr(int i,int len) throws Exception {
        SeqString t = new SeqString(count);
        if(i<0|| len<0 || i+len-1>=count){
            throw new Exception("参数错误");
        }
        for (int j = i; j <i+len; j++) {
            t.value[j-i]=value[j];
        }
        t.count= len;
        return t;
    }
    //在父串大第一个位置之前插入一个子串，返回当时的对象
    public SeqString Insert(int i, SeqString s) throws Exception {
        if (i<0 || i>count){
            throw new Exception("参数错误");
        }
        for (int j = count-1; j >=i; j--) {
            value[j+s.count] = value[j];
        }
        for (int k = i; k <i+s.count ; k++) {
            value[k] = s.value[k-i];
        }
        count+=s.count;
        return this;
    }
    //s删除子串，返回当前对象
    public SeqString Delete(int i,int len) throws Exception {
        if(i<0|| len<0 || i+len-1>=count){
            throw new Exception("参数错误");
        }else{
            for (int j = i+len; j < count; j++) {
                value[j-len] = value[j];
            }
            count-=len;
            return this;
        }
    }
    //打印
    public void print(){
        for (int i = 0; i < count; i++) {
            System.out.print(value[i]+" ");
        }
    }
}
